package controller;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Vector;

import model.Kunde;
import query.KundeGetRowQuery;
import query.KundeInsertQuery;
import query.KundeSuchenQuery;

public class ControllerKunde implements java.io.Serializable{

	private static final long serialVersionUID = -588911383972619474L;

	public static Vector<Kunde> getData(){
		try{
			//Socket erstellen
			Socket socket = new Socket("20.49.184.100", 5550);

			//Outputstream erstellen
			ObjectOutputStream clientOutputStream = new ObjectOutputStream(
					socket.getOutputStream());
			//Inputstream erstellen
			ObjectInputStream clientInputStream = new ObjectInputStream(
					socket.getInputStream());
			//Controller KundeSuchenQuery erstellen
			KundeSuchenQuery ks = new KundeSuchenQuery();
			//Controller Objekt an Server senden
			clientOutputStream.writeObject(ks);
			//Antwort von Server erhalten
			Object inputObj = clientInputStream.readObject();
			Vector<Kunde> vector = (Vector<Kunde>) inputObj;
			return vector;
		} catch (Exception e){
			
		}
		
		return null;
	}
	
	public static Kunde getKunde(int id){
		try{
			//Socket erstellen
			Socket socket = new Socket("20.49.184.100", 5550);
			
			//Outputstream erstellen
			ObjectOutputStream clientOutputStream = new ObjectOutputStream(
					socket.getOutputStream());
			
			//Inputstream erstellen
			ObjectInputStream clientInputStream = new ObjectInputStream(
					socket.getInputStream());
			
			//Controller KundeSuchenQuery erstellen
			KundeGetRowQuery kgr = new KundeGetRowQuery();
			kgr.setId(id);
			
			//Controller Objekt an Server senden
			clientOutputStream.writeObject(kgr);
	
			//Antwort von Server erhalten
			Object inputObj = clientInputStream.readObject();
			return (Kunde) inputObj;
		} catch (Exception e){
			
		}
		
		return null;
	}
	
	public static Kunde insertKunde(Kunde kunde){
		try{
			//Socket erstellen
			Socket socket = new Socket("20.49.184.100", 5550);
			
			//Outputstream erstellen
			ObjectOutputStream clientOutputStream = new ObjectOutputStream(
					socket.getOutputStream());
			
			//Inputstream erstellen
			ObjectInputStream clientInputStream = new ObjectInputStream(
					socket.getInputStream());
			
			//Controller KundeSuchenQuery erstellen
			KundeInsertQuery ki = new KundeInsertQuery(kunde);
			
			//Controller Objekt an Server senden 
			clientOutputStream.writeObject(ki);
	
			//Antwort von Server erhalten
			Object inputObj = clientInputStream.readObject();
			return (Kunde) inputObj;
		} catch (Exception e){
			
		}
		return null;
	}
	
}
